package com.haoshui.controller;

import com.haoshui.domain.ResponseResult;
import com.haoshui.domain.entity.Menu;
import com.haoshui.domain.vo.MenuListVo;
import com.haoshui.domain.vo.MenuTreeVo;
import com.haoshui.domain.vo.MenuUpdateVo;
import com.haoshui.domain.vo.RoleMenuTreeVo;
import com.haoshui.enums.AppHttpCodeEnum;
import com.haoshui.service.MenuService;
import com.haoshui.service.RoleMenuService;
import com.haoshui.utils.BeanCopyUitls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class MenuController {

    @Autowired
    private MenuService menuService;


    /**
     * 查询所有的菜单，可以根据给定参数模糊查询
     * @param status
     * @param menuName
     * @return
     */
    @GetMapping("system/menu/list")
    @PreAuthorize("@ps.hasPermission('system:menu:list')")
    public ResponseResult listMenu(String status, String menuName){
        //带条件查询menu
        List<Menu> menus =  menuService.listMenuByCondition(status, menuName);

        //封装Vo
        List<MenuListVo> menuListVos = BeanCopyUitls.copyBeanList(menus, MenuListVo.class);

        return ResponseResult.okResult(menuListVos);

    }


    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @PostMapping("system/menu")
    @PreAuthorize("@ps.hasPermission('system:menu:add')")
    public ResponseResult addMenu(@RequestBody Menu menu){
        //添加menu
        boolean b =  menuService.addMenu(menu);

        if(b){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.INSERT_FAILED);
        }

    }


    /**
     * 修改菜单，查询菜单详情步骤
     */
    @GetMapping("system/menu/{id}")
    @PreAuthorize("@ps.hasPermission('system:menu:edit')")
    public ResponseResult findMenu(@PathVariable("id") Long id){
        //查询菜单
        Menu menu =  menuService.findMenuByMenuId(id);

        //封装Vo
        MenuUpdateVo menuUpdateVo = BeanCopyUitls.copyBean(menu, MenuUpdateVo.class);

        return ResponseResult.okResult(menuUpdateVo);

    }


    /**
     * 修改菜单，更新操作
     * @param menu
     * @return
     */
    @PutMapping("system/menu")
    @PreAuthorize("@ps.hasPermission('system:menu:edit')")
    public ResponseResult updateMenu(@RequestBody Menu menu){
        //先判断上级菜单不能是自己
        if(menu.getId().equals(menu.getParentId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.MENU_PARENT_EQ_SELF);
        }

        //更新Menu
        boolean b = menuService.updateById(menu);

        if(b){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAILED);
        }
    }

    /**
     * 逻辑删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("/system/menu/{menuId}")
    @PreAuthorize("@ps.hasPermission('system:menu:remove')")
    public ResponseResult deleteMenu(@PathVariable("menuId") Long id){
        //先判断要删除的菜单有没有子菜单
        boolean b =  menuService.hasChildren(id);
        if(b) {
            return ResponseResult.errorResult(AppHttpCodeEnum.MENU_HAS_CHILDREN);
        }

        //删除菜单
        boolean b1 = menuService.removeById(id);

        if(b1){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAILED);
        }

    }


    /**
     * 添加角色，先查询所有的menu
     * @return
     */
    @GetMapping("/system/menu/treeselect")
    @PreAuthorize("@ps.hasPermission('system:role:add')")
    public ResponseResult checkTreeMenu(){
        //获取Menu树
        List<MenuTreeVo> menuTreeVoList =  menuService.getMenuTree();

        return ResponseResult.okResult(menuTreeVoList);
    }


    @Autowired
    private RoleMenuService roleMenuService;


    /**
     * 修改角色信息的时候，需要先查出角色信息
     * @param id
     * @return
     */
    @GetMapping("/system/menu/roleMenuTreeselect/{id}")
    @PreAuthorize("@ps.hasPermission('system:role:edit')")
    public ResponseResult roleMenuTreeselect(@PathVariable("id") Long id){
        //获取menu树
        List<MenuTreeVo> menuTree = menuService.getMenuTree();

        //获取菜单列表
        List<Long> menus = roleMenuService.selectMenusByRoleId(id);

        //封装Vo
        RoleMenuTreeVo result = new RoleMenuTreeVo(menuTree, menus);

        return ResponseResult.okResult(result);
    }



}
